//
//  AddViewController.m
//  Daybook
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "AddViewController.h"
#import "Common.h"
#import "AccountBook.h"

@interface AddViewController ()<AddViewDelegate , EditViewControllerDelegate>
@property (nonatomic, strong) AddView *addView;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) NSMutableString *str;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSString *remarks;
@property (nonatomic, assign) BOOL isChange;
@end
@implementation AddViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.hidesBackButton = YES;
    self.str = @"".mutableCopy;
    
    NSString *string = [_addView.textField.text substringFromIndex:1];
    if (![_str isEqualToString:string] && ![string isEqualToString:@"0.00"]) {
        _str = [NSMutableString stringWithString:string];
    }
    
    _addView.str = _str;
    _addView.isClick = NO;
    self.topView.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [_addView.textField becomeFirstResponder];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.navigationItem.hidesBackButton = NO;
    self.topView.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self createView];
    

    
}

- (void)createView {
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, MAIN_WIDTH, MAIN_HEIGHT / 20)];
    [self.navigationController.navigationBar addSubview:_topView];
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"收入", @"支出"]];
    _seg.selectedSegmentIndex = 1;
    _seg.frame = CGRectMake(0, 0, MAIN_WIDTH / 3, MAIN_HEIGHT / 25);
    _seg.center = CGPointMake(_topView.center.x, MAIN_HEIGHT / 40);
    [_topView addSubview:_seg];
    [_seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    _seg.tintColor = [UIColor orangeColor];
    self.addView = [[AddView alloc] initWithFrame:self.view.frame];
    _addView.delegate = self;
    [self.view addSubview:_addView];
    
}
- (void)segAction:(UISegmentedControl *)seg {
    self.isChange = 1;
    _addView.isChange = _isChange;
    if (seg.selectedSegmentIndex == 1) {
        _addView.isState = YES;
    }
    else {
        _addView.isState = NO;
    }
}

- (void)popViewActionPic:(NSString *)pic describe:(NSString *)describe price:(NSString *)price {
//    NSLog(@"describer: hahaha: %@", describe);
    // 返回前一页
    CATransition *backPageAnimation = [CATransition animation];
    backPageAnimation.duration = 0.5;
    backPageAnimation.type = kCATransitionMoveIn;
    backPageAnimation.subtype = kCATransitionFromTop;
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.view.layer addAnimation:backPageAnimation forKey:nil];
    [self.navigationController popToRootViewControllerAnimated:NO];
    //self.image是图片,self.remarks是备注
    if (price != nil) {
        if (_seg.selectedSegmentIndex == 1) {
            NSLog(@"支出");

//            NSLog(@"%@", self.remarks);
            
            RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
            DayBook *currentDayBook = [result firstObject];

            Account *accountBook = [[Account alloc]init];
            

            
            accountBook.picName = pic;
            accountBook.name = describe;

            NSMutableString *priceStr = [NSMutableString stringWithString:price];
            [priceStr substringFromIndex:1];
            accountBook.money = (NSNumber *)price;
            accountBook.type = @"2";
            accountBook.income = 0;
            accountBook.time = [NSDate dateWithTimeIntervalSinceNow:0];
            accountBook.remarks = self.remarks;
            
            accountBook.imageData = UIImagePNGRepresentation(self.image);
            
            
            RLMRealm *realm = [RLMRealm defaultRealm];
            [realm beginWriteTransaction];
            [currentDayBook.bookContent addObject:accountBook];
//            NSLog(@"我是imageData%@",accountBook.imageData);
            [realm addObject:currentDayBook];
            [realm commitWriteTransaction];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"moneyChanged" object:nil];

            
        }
        else {
            NSLog(@"收入");
            
            RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
            DayBook *currentDayBook = [result firstObject];
            Account *accountBook = [[Account alloc]init];
            
            
            accountBook.picName = pic;
            accountBook.name = describe;
            NSMutableString *priceStr = [NSMutableString stringWithString:price];
            [priceStr substringFromIndex:1];
            accountBook.income = (NSNumber *)priceStr;
            accountBook.type = @"1";
            accountBook.money = 0;
            accountBook.time = [NSDate dateWithTimeIntervalSinceNow:0];
            accountBook.remarks = self.remarks;
            accountBook.imageData = UIImagePNGRepresentation(self.image);
//            NSLog(@"%@", accountBook.imageData);
            
            RLMRealm *realm = [RLMRealm defaultRealm];
            [realm beginWriteTransaction];
            [currentDayBook.bookContent addObject:accountBook];
            [realm addObject:currentDayBook];
            [realm commitWriteTransaction];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"incomeChanged" object:nil];
            
        }
        
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"rightViewExchangeAccountBook" object:nil];
}

- (void)jumpToEditView {
    EditViewController *editVC = [EditViewController new];
    [self.navigationController pushViewController:editVC animated:YES];
    editVC.delegate = self;
}

- (void)sendpic:(UIImage *)pic remarks:(NSString *)remarks {
    self.image = pic;
    self.remarks = remarks;
}


@end
